package cn.iocoder.yudao.module.measure.events.listener;

import cn.iocoder.yudao.module.measure.dal.dataobject.warning.BusinessDeviceWarnPersonConfig;
import cn.iocoder.yudao.module.measure.dal.dataobject.warning.BusinessDeviceWarnRecord;
import cn.iocoder.yudao.module.measure.events.BusinessDeviceWarnRecordEvent;
import cn.iocoder.yudao.module.measure.service.protocol.TemplateDeviceUploadDataConfigService;
import cn.iocoder.yudao.module.measure.service.warning.BusinessDeviceUploadWarningConfigService;
import cn.iocoder.yudao.module.measure.service.warning.BusinessDeviceWarnRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.time.format.DateTimeFormatter;
import java.util.List;

@Component
@Slf4j
public class DeviceWarningListener implements ApplicationListener<BusinessDeviceWarnRecordEvent> {


    @Autowired
    private TemplateDeviceUploadDataConfigService warningConfigService;

//    @Autowired
//    private MsgApi msgApi;


    private final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");


    @Autowired
    private BusinessDeviceWarnRecordService warnRecordService;

    @Autowired
    private BusinessDeviceUploadWarningConfigService uploadWarningConfigService;



    @Async("applicationMeasureEventTaskExecutor")
    @Override
    public void onApplicationEvent(BusinessDeviceWarnRecordEvent event) {
        BusinessDeviceWarnRecord payload = event.getSource();
        Long measureId = payload.getMeasureId();
        Long tenantId =  payload.getTenantId();
        // 报警内容增加设备信息头

        // 储存报警记录表
        BusinessDeviceWarnRecord businessDeviceWarnRecord = new BusinessDeviceWarnRecord();
        businessDeviceWarnRecord.setMeasureId(measureId);
        businessDeviceWarnRecord.setTenantId(payload.getTenantId());
        businessDeviceWarnRecord.setWarningContent(payload.getWarningContent());
        businessDeviceWarnRecord.setDeviceTime(payload.getDeviceTime());
        businessDeviceWarnRecord.setWarningLevel(payload.getWarningLevel());
        businessDeviceWarnRecord.setWarningCheckType(payload.getWarningCheckType());
        businessDeviceWarnRecord.setDeviceData(payload.getDeviceData());
        businessDeviceWarnRecord.setCreatedBy(Long.valueOf(0L));//由系统创建
        // 生成设备报警记录
        warnRecordService.save(tenantId,businessDeviceWarnRecord);
        // 发送报警消息
        String measureName = uploadWarningConfigService.getMeasureNameStr(measureId);
        String readableWarningContent = "监测点"+measureName+"于"+payload.getCreatedTime().format(dateTimeFormatter)+"发生"+payload.getWarningContent();
        // 根据监测点id取对应的收信人
        List<BusinessDeviceWarnPersonConfig> list = uploadWarningConfigService.getWarningPerson(measureId);
        for(BusinessDeviceWarnPersonConfig item : list){
            // TODO 实现报警消息发送
        }
    }
}
